﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Prime.Models;
using NLog;

namespace Prime.Controllers
{
    public class LessonController : ApiController
    {
        private LearningDbEntities db = new LearningDbEntities();
        private static Logger logger = LogManager.GetCurrentClassLogger();


        
        // GET api/Lesson
        [Authorize]
        public IQueryable<Lesson> GetLessons()
        {
            return db.Lessons;
        }
        [Authorize]
        // GET api/Lesson/5
        [ResponseType(typeof(Lesson))]
        public IHttpActionResult GetLesson(int id)
        {
            Lesson lesson = db.Lessons.Find(id);

            var materials = from t in db.LessonMaterials
                            where t.LessonId == id
                            select new { Data = t, type = "Material" };
            var questions = from t in db.LessonQuestions
                            where t.LessonId == id
                            select new { Data = t, type = "Question" };
            if (lesson == null)
            {
                return NotFound();
            }
            var ret = new
            {
                LessonInfo = lesson,
                Materials = materials,
                Questions = questions
            };
            
            logger.Info("Started lesson: " + id);
            return Ok(ret);
        }

        // PUT api/Lesson/5
        [Authorize]
        public IHttpActionResult PutLesson(int id, Lesson lesson)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != lesson.LessonId)
            {
                return BadRequest();
            }

            db.Entry(lesson).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LessonExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        [Authorize]
        public IHttpActionResult PutLesson(string message){
            //TODO log message
            logger.Info(message);
            return Ok();
        }
        [Authorize]
        // POST api/Lesson
        [ResponseType(typeof(Lesson))]
        public IHttpActionResult PostLesson(Lesson lesson)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Lessons.Add(lesson);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = lesson.LessonId }, lesson);
        }

        // DELETE api/Lesson/5
        [ResponseType(typeof(Lesson))]
        public IHttpActionResult DeleteLesson(int id)
        {
            Lesson lesson = db.Lessons.Find(id);
            if (lesson == null)
            {
                return NotFound();
            }

            db.Lessons.Remove(lesson);
            db.SaveChanges();

            return Ok(lesson);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool LessonExists(int id)
        {
            return db.Lessons.Count(e => e.LessonId == id) > 0;
        }


    }
}